class Solution {
private:
    set<int> set;
public:
    bool isHappy(int n) {
        while(n != 1){
            auto it = set.find(n);
            if(it != set.end()){
                return false;
            }
            set.insert(n);
            n = isHappyStep(n);
            
        }

        return true;
    }

    int isHappyStep(int n) {
        int next = 0;
        while(n != 0) {
            next = next + (n % 10) * (n % 10);
            n = n / 10;
        }

        return next;
    }
};